﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking; 
using System.Threading;
using Taobao.Tools2.Domain.EntityFramework;
using Taobao.Tools2.Domain;
using Taobao.Common;
using System.Configuration;

namespace Taobao.Tools2
{
    public partial class MainOperationForm : DockContent
    {
        #region Ctor

        public MainOperationForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public int TotalDashBoardCount
        {
            get;
            set;
        }

        private List<int> ProcessShopIDList
        {
            get;
            set;
        }

        protected EfDbContext Context { get; set; }
        protected IRepository<Shop> ShopRepository { get; set; }

        #endregion

        #region Load

        private void MainOperationForm_Load(object sender, EventArgs e)
        { 
            ProcessShopIDList = new List<int>();
            Context = new EfDbContext();
            ShopRepository = new EntityRepository<Shop>(Context);
            {
                bool useTop = true;
                string useTopValue = ConfigurationManager.AppSettings[Constants.UseTopKey];
                if (!String.IsNullOrEmpty(useTopValue))
                {
                    Boolean.TryParse(useTopValue, out useTop);
                }
                string urlKey = useTop ? Constants.TopUrlKey : Constants.SandBoxUrlKey;
                var requestUrl = ConfigurationManager.AppSettings[urlKey];

                // TODO: I only consider the only one panel in the dashboard.    
                //var shop = ResourceManager.Current.Entities.Shop.First(); 
                var shop = ShopRepository.FindAll().AsQueryable().FirstOrDefault();
                if (shop == null)
                {
                    MessageBox.Show("数据库里没有店铺信息，请先输入店铺信息后再运行程序。");
                    return;
                }

                var processor = new TaobaoToolsProcesser(requestUrl, "Tools", shop.ID);
                ProcessShopIDList.Add(shop.ID);
                processor.SwitchShop = sh =>
                {
                    var shopid = sh.ID;
                    try
                    {
                        var shopIDs = ShopRepository.FindAll().AsQueryable().Select(s => s.ID).ToList();
                        var s1 = shopIDs.FirstOrDefault(sp =>
                        {
                            return !ProcessShopIDList.Contains(sp);
                        });
                        if (s1 == 0)
                        {
                            if (shopIDs.Count > 0)
                                shopid = shopIDs[0];
                            else
                                shopid = sh.ID;
                            ProcessShopIDList.Clear();
                        }
                        else
                        {
                            shopid = s1;
                        }
                        ProcessShopIDList.Add(shopid);
                        return shopid;

                    }
                    catch { }
                    return shopid;
                };
                DashBoardControl.TaobaoProcessor = processor;
            }          
               
        }
        
        #endregion

        #region Processor Events
        
        #endregion

        protected override void OnClosed(EventArgs e)
        {
            Context.Dispose();
            base.OnClosed(e);
        }
         
    }
}
